I am attempting to use the R package tigris within an R tool, in order to retrieve geographies using the Census API. The attached R code (saved as txt to allow upload) works as expected in RStudio. Note that tigris is not a package included in the Predictive Analytics RInstaller, but the package is correctly installed and available to Alteryx. Running the R tool produces the following error: The Designer x64 reported: InboundNamedPipe GetOverlappedResults: The pipe has been ended.
Note that on occasion, the error message also includes “You have found a bug. Replicate, then let us know. We shall fix it soon.” I opened a case, but was told that since the package is not part of the Predictive Analytics RInstaller that it is very unlikely the Dev team would address, so the case was closed.
Any advice on the potential issue would be helpful.
Huge thanks to PaulNo for assistance in finding a workable solution!
See the comments and solution in the R tigris package Inbound Pipe Error - Alteryx Community Solution post, and detailed in the code example below.
Load required packages.
library(tigris)
library(sf)
library(geojsonsf)
library(dplyr)
library(magrittr)
library(tibble)
library(tidyr)
library(mapview)
library(kableExtra)Use the tigris tracts() function to download all tracts in a specific state and county.
tr_WI_Winnebago <- tigris::tracts(
state = "WI", # Specify state
county = "Winnebago", # Specify county
year = 2019, # Specify year
cb = FALSE) # Specify cartographic boundary (True) or TIGER Line/Shapefile (False)
class(tr_WI_Winnebago) # Check object class
st_crs(tr_WI_Winnebago) # Check coordinate systemBy default, tigris returns sf (simple feature) objects, which is not a useable object type in Alteryx workflows outside of the R tool, because Alteryx Designer uses data-frame-like structures in its data streams. Therefore, we need to convert the sf object to a data frame.
Use the geojsonsf package to convert the sf geometry to a geojson vector; then convert the geojson vector to a data frame.
geometry <- geojsonsf::sfc_geojson(tr_WI_Winnebago$geometry) # Converts `sfc` objects to GeoJSON
# as.list(geometry) # View as a list
st_crs(geometry) # Check coordinate system
# The coordinate reference system for all GeoJSON coordinates is a geographic coordinate reference system, using the World Geodetic System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units of decimal degrees.
geometry_df <- data.frame(GEOID = tr_WI_Winnebago$GEOID,
geometry = matrix(unlist(geometry),nrow = length(geometry), byrow = T)) # Create data frame from geojson vector, maintaining the GEOID and geometry variable
class(geometry_df) # Check object class
st_crs(geometry_df) # Check coordinate systemRemove the original sf geometry and join the original attribute data to the new data frame.
tr_WI_Winnebago_removesf <- tr_WI_Winnebago %>% st_set_geometry(NULL)
class(tr_WI_Winnebago_removesf) # Check object class
st_crs(tr_WI_Winnebago_removesf) # Check coordinate system
tr_WI_Winnebago_geo <- tr_WI_Winnebago_removesf %>%
inner_join(geometry_df, by = c("GEOID" = "GEOID"))
class(tr_WI_Winnebago_geo) # Check object class
st_crs(tr_WI_Winnebago_geo) # Check coordinate systemIn Alteryx, write out to an output anchor; then use a Select tool to convert geometry to a spatial object.
A work by Alex Brasch